package com.example.springboot.prop;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * springboot自动支持读取application.properties的内容
 * <p> 支持的数据结构：
 *  字面值：字符串，布尔类型，数值，日期。字符串默认不加引号，单引号会转义特殊字符。日期格式支持 yyyy/MM/dd HH:mm:ss
 *
 * <p> {@link ConfigurationProperties }的优点：
 *  1. 支持批量的注入属性，只需要指定一个前缀 prefix ;
 *  2. 支持复杂的数据类型，比如 List 、 Map ;
 *  3. 对属性名匹配的要求较低，比如 user-name ， user_name ， userName ， USER_NAME 都可以取值 ;
 *  4. 支持JAVA的JSR303数据校验 。
 * <p>
 * @author Zhang.Z
 * @version 2025-09-19
 */

@ConfigurationProperties(prefix = "userinfo")
public class DefaultProperty {

    private String username;
    private Integer age;
    private Boolean active;
    private Map<String, Object> map;
    private Date createdDate;
    private List<String> hobbies;
    private LocalDateTime jdk8date1;

    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    private LocalDateTime jdk8date2;

    private Score score;


    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Boolean getActive() {
        return active;
    }

    public void setActive(Boolean active) {
        this.active = active;
    }

    public Map<String, Object> getMap() {
        return map;
    }

    public void setMap(Map<String, Object> map) {
        this.map = map;
    }

    public Date getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    public List<String> getHobbies() {
        return hobbies;
    }

    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }

    public LocalDateTime getJdk8date1() {
        return jdk8date1;
    }

    public void setJdk8date1(LocalDateTime jdk8date1) {
        this.jdk8date1 = jdk8date1;
    }

    public LocalDateTime getJdk8date2() {
        return jdk8date2;
    }

    public void setJdk8date2(LocalDateTime jdk8date2) {
        this.jdk8date2 = jdk8date2;
    }

    public Score getScore() {
        return score;
    }

    public void setScore(Score score) {
        this.score = score;
    }

    public static class Score{

        private Integer score;
        private String level = "fail";
        private Boolean valid = false;

        public Score() {
        }

        public Score(Integer score) {
            this.score = score;
        }

        public Score(Integer score, String level) {
            this.score = score;
            this.level = level;
        }

        public Score(Integer score, Boolean valid) {
            this.score = score;
            this.valid = valid;
        }

        public Score(Integer score, String level, Boolean valid) {
            this.score = score;
            this.level = level;
            this.valid = valid;
        }

        public Integer getScore() {
            return score;
        }

        public Score setScore(Integer score) {
            this.score = score;
            return this;
        }

        public String getLevel() {
            return level;
        }

        public Score setLevel(String level) {
            this.level = level;
            return this;
        }

        public Boolean getValid() {
            return valid;
        }

        public Score setValid(Boolean valid) {
            this.valid = valid;
            return this;
        }

        @Override
        public String toString() {
            return "Score{" +
                    "score=" + score +
                    ", level='" + level + '\'' +
                    ", valid=" + valid +
                    '}';
        }
    }
}
